import { changeCustomerStatusApi, updateCustomerPicimgShowApi } from "@/api/client/documentManagement/waybillManagement";
import { deleteFileByIdApi } from "@/api/common";
import { message } from "ant-design-vue";

export default function OperationHook(state: Base.GeneraDataType) {
  const { dialogOpts, formState, passData, downloadFileByUrl, init, drawerOpts, formItemColumnMap, formItemColumn, loading } = state;

  const modifyWaybillHandle = (row: Base.GeneraDataType) => {
    dialogOpts.title = `修改运单(${row.waybillNum})`;
    dialogOpts.type = 2;
    dialogOpts.passData = row;
    dialogOpts.component = "ModifyWaybill";
    dialogOpts.visible = true;
  };
  const modifyTimeHandle = () => {
    dialogOpts.width = "520px";
    dialogOpts.title = "修改期望取货时间";
    dialogOpts.type = 2;
    dialogOpts.passData = formState.value;
    dialogOpts.component = "ModifyTime";
    dialogOpts.visible = true;
  };
  const modifyWarehouseHandle = () => {
    dialogOpts.width = "820px";
    dialogOpts.title = "修改送货仓库";
    dialogOpts.type = 2;
    dialogOpts.passData = formState.value;
    dialogOpts.component = "ModifyWarehouse";
    dialogOpts.visible = true;
  };
  const modifyPickupAddressHandle = () => {
    dialogOpts.width = "820px";
    dialogOpts.title = "修改取件地址";
    dialogOpts.type = 2;
    dialogOpts.passData = formState.value;
    dialogOpts.component = "ModifyPickupAddress";
    dialogOpts.visible = true;
  };
  // 过滤处理 交税方式
  const fltDeclarationType = (row: Array<{ code: string; name: string }>) => {
    if (!row || !row.length) return "-";
    const result = row.map((t: { code: string; name: string }) => t.name);
    return result.join(",");
  };
  const deleteFileHandle = (file: ClientWaybillManagement.FileListRow) => {
    return deleteFileByIdApi({ id: file.id, fileUrl: file.fileUrl }).then((res: any) => {
      const { code } = res;
      if (code === 200) {
        message.success("删除成功!");
        init(passData.value.id);
      }
    });
  };
  //页脚合计
  const sumNum = (list: BusinessWaybill.ListRows[], field: string) => {
    let count = 0;
    list.forEach(item => {
      count += (Number(item[field]) * 100) / 100;
    });
    return count.toFixed(2);
  };
  const footerMethod = ({ columns, data }: { columns: BusinessWaybill.ListRows[]; data: any[] }) => {
    return [
      columns.map((column, columnIndex) => {
        if (columnIndex === 0) {
          return "合计";
        }
        if (["pickingBoxWeight", "volumeWeight"].includes(column.field)) {
          return `${sumNum(data, column.field)}`;
        }
        return null;
      })
    ];
  };

  const downloadFileHandle = (file: ClientWaybillManagement.FileListRow) => {
    downloadFileByUrl(file.fileUrl, file.filename);
  };
  const uploadFileHandle = () => {
    dialogOpts.width = "520px";
    dialogOpts.title = "附件上传";
    dialogOpts.type = 1;
    dialogOpts.passData = [passData.value];
    dialogOpts.component = "BatchUploadFile";
    dialogOpts.visible = true;
  };
  // 查看发票
  const showInvoiceHandle = () => {
    drawerOpts.width = "1200px";
    drawerOpts.title = "查看发票";
    drawerOpts.type = 1;
    drawerOpts.passData = passData.value;
    drawerOpts.component = "ShowInvoice";
    drawerOpts.visible = true;
  };
  // 修改发票
  const modifyInvoiceHandle = () => {
    drawerOpts.width = "1200px";
    drawerOpts.title = "修改发票";
    drawerOpts.type = 2;
    drawerOpts.passData = passData.value;
    drawerOpts.component = "MakeInvoice";
    drawerOpts.visible = true;
  };

  // 设置转单号
  const SetTransferNumberHandle = () => {
    drawerOpts.width = "1200px";
    drawerOpts.title = "设置转单号";
    drawerOpts.passData = formState.value;
    drawerOpts.component = "SetNumber";
    drawerOpts.visible = true;
  };

  // 修改客户备注
  const modifyRemarkHandle = () => {
    dialogOpts.title = `修改客户备注`;
    dialogOpts.width = "520px";
    dialogOpts.type = 1;
    dialogOpts.component = "ModifyRemark";
    dialogOpts.passData = [formState.value];
    dialogOpts.visible = true;
  };
  // 修改客户备注
  const modifyInnerRemarkHandle = () => {
    dialogOpts.title = `修改内部备注`;
    dialogOpts.width = "520px";
    dialogOpts.type = 2;
    dialogOpts.component = "ModifyRemark";
    dialogOpts.passData = [formState.value];
    dialogOpts.visible = true;
  };
  //修改目的地
  const modifyCountryRegionNameHandle = () => {
    dialogOpts.title = `修改目的地`;
    dialogOpts.width = "520px";
    dialogOpts.component = "ModifyCountryRegionName";
    dialogOpts.passData = passData.value;
    dialogOpts.visible = true;
  };

  const showOrRetract = () => {
    if (formItemColumnMap.value.length > 3) {
      formItemColumnMap.value = formItemColumnMap.value.slice(0, 3);
    } else {
      formItemColumnMap.value = formItemColumn;
    }
  };

  //客户可见图片
  const isShowPhotoHandle = (e: boolean) => {
    loading.value = true;
    return updateCustomerPicimgShowApi({ status: e ? 1 : 0, id: formState.value.id })
      .then(({ code }: any) => {
        code === 200 && message.success("操作成功");
        init(passData.value.id);
      })
      .finally(() => {
        loading.value = false;
      });
  };

  //客户可见
  const changeStatus = (e: boolean, row: any) => {
    const params = {
      id: row.id,
      customerShow: e ? 1 : 0
    };
    loading.value = true;
    changeCustomerStatusApi(params)
      .then(({ code }: any) => {
        code === 200 && message.success("操作成功");
        init(passData.value.id);
      })
      .finally(() => {
        loading.value = false;
      });
  };
  return {
    modifyWaybillHandle,
    modifyTimeHandle,
    modifyWarehouseHandle,
    modifyPickupAddressHandle,
    fltDeclarationType,
    deleteFileHandle,
    downloadFileHandle,
    uploadFileHandle,
    showInvoiceHandle,
    modifyInvoiceHandle,
    SetTransferNumberHandle,
    modifyRemarkHandle,
    modifyInnerRemarkHandle,
    modifyCountryRegionNameHandle,
    footerMethod,
    showOrRetract,
    isShowPhotoHandle,
    changeStatus
  };
}
